<template>
	<view class='main-container'>
		<!--搜索栏开始-->
		<view class='search' >
			<view class="sr" @tap='search'>
				<image src='../../static/images/search1.png'></image>
				<text> 搜索商品</text>
			</view>
		</view>
		<!-- 导航栏开始 -->
		<view class='banner' :style="top">
			<swiper indicator-dots="true" interval="4000" circular="true" autoplay="true" indicator-active-color="#fff"
			 indicator-color="#dcdcdc">
				<swiper-item v-for="(item,index)  in banners " :key="index">
					<image :src='item.imageUrl'></image>
				</swiper-item>
			</swiper>
		</view>
		<!-- 分类开始 -->
		<view class='category'>
			<view v-for="(item,index)  in categorys" :key="index" @tap="go_category(item.url)">
				<image :src='item.imageUrl'></image>
				<text>{{item.title}}</text>
			</view>
		</view>
		<view class="title">
			猜你喜欢
		</view>
		<!--商品列表开始-->
		<view class="goodslist">
			<view class="goods" v-for="(item,index) in goods" :key="index" @tap="go_detail(index)">
				<image :src="getRealPath(item.goodsImgUrl)" mode=""></image>
				<view class="name">{{item.goodsName}}</view>
				<view class="bottom">
					<view class="price">￥{{item.goodsPrice}}</view>
					<view class="time">{{formatTime(item.createTime)}}</view>
				</view>
			</view>
		</view>
		<view v-if="goods.length!=0" style="margin-bottom: 120upx;height: 80upx; font-size: 28upx;color: #999999;text-align: center; line-height: 80upx;width: 92;">{{content}}</view>
		<view class="tab">
			<view @tap="go_add()">
				<image src="../../static/images/addgoods.png" mode=""></image>
				<text>发布二手</text>
			</view>
			<view @tap="go_collection()">
				<image src="../../static/images/myshoucang.png" mode=""></image>
				<text>已收藏</text>
			</view>
			<view @tap="go_mygoods()">
				<image src="../../static/images/mygoods.png" mode=""></image>
				<text>我的二手</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import utils from "../../static/js/utils.js"
	let top;
	// #ifdef APP-PLUS
	const device = uni.getSystemInfoSync();
	top = device.statusBarHeight + uni.upx2px(90);
	// #endif
	// #ifdef MP-WEIXIN
	 top = uni.upx2px(80) + 76;
	// #endif
	export default {
		computed: {
			...mapState(['goodsChange']),
			top() {
				return "margin-top:" + top + "px";
			}
		},
		data() {
			return {
				goods:[],
				banners: [{
						id: 1,
						imageUrl: 'http://image.kevinproject.cn/11.jpg'
					}, {
						id: 2,
						imageUrl: 'http://image.kevinproject.cn/22.jpg'
					},
					{
						id: 3,
						imageUrl: 'http://image.kevinproject.cn/33.jpg'
					}
				],
				categorys: [{
						id: 1,
						imageUrl: '../../static/images/shouji.png',
						title: '手机·数码',
						url: 'category?typeId=2&typeName=数码产品'
					},
					{
						id: 2,
						imageUrl: '../../static/images/shenghuo.png',
						title: '生活·化妆品',
						url: 'category?typeId=1&typeName=生活用品'
					},
					{
						id: 3,
						imageUrl: '../../static/images/tushu.png',
						title: '图书·资料',
						url: 'category?typeId=3&typeName=图书资料'
					}, {
						id: 4,
						imageUrl: '../../static/images/piaowu.png',
						title: '票·优惠券',
						url: 'category?typeId=8&typeName=优惠券'
					},
					{
						id: 5,
						imageUrl: '../../static/images/qita.png',
						title: '全部分类',
						url: 'allcategory'
					}
				],
				pageId:0,
				content:''
			}
		},
		onReachBottom() {
			if (this.content != "已经到底了") {
				let pId = this.pageId;
				this.getGoods(pId + 1, 1);
			}
		
		},
		onLoad() {
			this.getGoods(0,0);
		},
		onShow() {
			if(this.goodsChange) {
				this.getGoods(0,0);
			}
			
		},
		onPullDownRefresh() {
			this.getGoods(0, 0);
		},
		methods: {
			go_category(url){
				uni.navigateTo({
					url: url
				});
			},
			formatTime(timestamp) {
				var da = new Date(timestamp);
				return (da.getMonth() + 1) + "-" + da.getDate();
			},
			getGoods(pageId, type) {
				let that = this;
				utils.get("market/index", {
					pageId
				}, function(data) {
					if (type == 0) {
						that.content = "";
						that.goods = data;
						uni.stopPullDownRefresh();
					} else {
						if (data.length == 0) {
							that.content = "已经到底了"
						} else {
							that.goods.push.apply(that.goods, data);
							that.pageId++;
						}
			
					}
				});
			},
			go_detail(index){
				uni.navigateTo({
					url: 'goodsdetail?good='+JSON.stringify(this.goods[index])
				});
			},
			go_add(){
				uni.navigateTo({
					url: 'add'
				});
			},
			go_collection(){
				uni.navigateTo({
					url: 'collectgoods'
				});
			},
			go_mygoods(){
				uni.navigateTo({
					url: 'mygoods'
				});
			},
			
			getRealPath(path) {
				return utils.baseImgUrl + path;
			},
			search() {
				uni.navigateTo({
					url: 'searchgoods'
				});
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

<style>
	page {
		transition: all 0.3s linear;
	}

	.title {
		width: 100%;
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		color: #f47825;
		font-size: 30upx;
		margin-top: 20upx;

	}
	.tab{
		width: 100%;
		height: 110upx;
		display: flex;
		flex-direction: row;
		position: fixed;
		background-color: #ffffff;
		z-index:999;
		justify-content: space-between;
		align-items: center;
		bottom: 0;
		box-shadow: inset 0px 1px 1px -1px #c8c7cc;
	}
	.tab view{
		width: 30%;
		height: 110upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.tab view image {
		height: 46upx;
		width: 46upx;
	}
	.tab view text {
		font-size: 24upx;
		margin-top: 4upx;
	}
	.goodslist {
		width: 92%;
		padding: 0 4% 3vw 4%;
		display: -webkit-box;
		display: -webkit-flex;
		display: -ms-flexbox;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		margin-top: 10upx;
		margin-bottom: 30upx;

	}

	.goods {
		width: 48%;
		border-radius: 20upx;
		background-color: #fff;
		margin: 0 0 15upx 0;
		box-shadow: 0upx 5upx 25upx rgba(0, 0, 0, 0.1);
		margin-top: 14upx;
	}

	.goods image {
		width: 100%;
		border-radius: 20upx 20upx 0 0;
		height: 166px;

	}

	.name {
		width: 92%;
		padding: 10upx 4%;
		text-align: justify;
		font-size: 30upx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.bottom {
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
		width: 92%;
		padding:10upx 4% 10upx 4%;


	}

	.price {
		color: #e65339;
		font-size: 30upx;
		font-weight: 600;
		

	}

	.time {
		color: #807c87;
		font-size: 24upx;


	}

	.main-container {
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.search {
		width: 100%;
		background: linear-gradient(to left, #fa4dbe 0, #fbaa58 100%);
		display: flex;
		flex-direction: row;
		align-items: center;
		position: fixed;
		top: 0;
		justify-content: center;
		z-index: 999;
		/* #ifdef MP-WEIXIN */
		padding-top: 66px;
		/* #endif */
		/* #ifdef APP-PLUS */
		padding-top: var(--status-bar-height);
		/* #endif */
	}

	.search .sr {
		width: 90%;
		height: 60upx;
		background-color: white;
		border-radius: 10upx;
		margin-left: 13upx;
		margin-top: 40upx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-bottom: 20upx;
	}

	.search .sr image {
		width: 42upx;
		height: 42upx;
	}

	.search view text {
		color: #777;
		font-size: 26upx;
		
	}

	.banner {
		width: 100%;
		height: 375upx;
	}

	.banner swiper {
		height: 375upx;
	}

	.banner image {
		width: 100%;
		height: 375upx;
	}

	.category {
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		height: 140upx;
		background-color: white;
	}

	.category view {
		width: 180upx;
		height: 140upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.category view image {
		width: 55upx;
		height: 55upx;
	}

	.category view text {
		font-size: 25upx;
		margin-top: 15upx;
		color: #666;
	}
</style>
